﻿using System;
using System.Collections.Generic;
using Lottery.App.Model;

namespace Lottery.App.DAL
{
    public class B5Z4Dao : ResultDao<B5Z4Info>
    {
        protected override IList<B5Z4Info> GetItems(RecordInfo record)
        {
            List<string> array = Utility.Utils.Get8P4(record.ASC_Result);

            string hql = "from B5Z4Info where (1=2)";
            foreach (string item in array)
            {
                hql += String.Format(" or Value like '%{0}%'", item.Replace(" ", "%"));
            }

            return Session.CreateQuery(hql).List<B5Z4Info>();
        }

        protected override int GetLastID(string value)
        {
            List<string> items = Utility.Utils.Get5P4(value);

            string hql = "select Max(r.LastID) from N4Info r where Value in (:val)";

            return Session.CreateQuery(hql)
                .SetParameterList("val", items)
                .UniqueResult<int>();
        }

        internal override IList<RecordInfo> List(string value)
        {
            List<string> items = Utility.Utils.Get5P4(value);

            string hql = "from RecordInfo where ASC_Result like ? or ASC_Result like ? or ASC_Result like ? or ASC_Result like ? or ASC_Result like ?";

            return Session.CreateQuery(hql)
                .SetString(0, MatchMode.Anywhere(items[0].Replace(" ", "%")))
                .SetString(1, MatchMode.Anywhere(items[1].Replace(" ", "%")))
                .SetString(2, MatchMode.Anywhere(items[2].Replace(" ", "%")))
                .SetString(3, MatchMode.Anywhere(items[3].Replace(" ", "%")))
                .SetString(4, MatchMode.Anywhere(items[4].Replace(" ", "%")))
                .List<RecordInfo>();
        }

        public override IList<int> GetIds(string value)
        {
            List<string> items = Utility.Utils.Get5P4(value);

            string hql = "select r.id from RecordInfo r where ASC_Result like ? or ASC_Result like ? or ASC_Result like ? or ASC_Result like ? or ASC_Result like ?";

            return Session.CreateQuery(hql)
                .SetString(0, MatchMode.Anywhere(items[0].Replace(" ", "%")))
                .SetString(1, MatchMode.Anywhere(items[1].Replace(" ", "%")))
                .SetString(2, MatchMode.Anywhere(items[2].Replace(" ", "%")))
                .SetString(3, MatchMode.Anywhere(items[3].Replace(" ", "%")))
                .SetString(4, MatchMode.Anywhere(items[4].Replace(" ", "%")))
                .List<int>();
        }

        public override IList<int> GetIds(string value, int start)
        {
            List<string> items = Utility.Utils.Get5P4(value);

            string hql = "select r.id from RecordInfo r where r.id > ? and (ASC_Result like ? or ASC_Result like ? or ASC_Result like ? or ASC_Result like ? or ASC_Result like ?)";

            return Session.CreateQuery(hql)
                .SetInt32(0, start)
                .SetString(1, MatchMode.Anywhere(items[0].Replace(" ", "%")))
                .SetString(2, MatchMode.Anywhere(items[1].Replace(" ", "%")))
                .SetString(3, MatchMode.Anywhere(items[2].Replace(" ", "%")))
                .SetString(4, MatchMode.Anywhere(items[3].Replace(" ", "%")))
                .SetString(5, MatchMode.Anywhere(items[4].Replace(" ", "%")))
                .List<int>();
        }
    }
}
